<?php
/**
 * 定义一组便于生成表单元控件的方法
 *
 * @copyright Copyright (c) 2005 - 2006 FleaPHP.org (www.fleaphp.org)
 * @author 廖宇雷 dualface@gmail.com
 * @package Core
 * @version $Id: Html.php 861 2007-06-01 16:37:41Z dualface $
*/


/**
 * 生成一个下拉列表框
 * 如果$arr格式为[id]=> name则为普通下拉表框
 * 如果$arr格式为[groupname]=>[id]=>name，则为包含optgroup的下拉表框
 * 
 * @param string $name
 * @param array $arr
 * @param mixed $selected
 * @param string $extra
 */
function html_dropdown_list($name, $arr, $selected = null, $extra = null) {
	!is_array($selected) && $selected = array($selected);
	
    echo "<select id='$name' name='{$name}' {$extra} >";
    
    
	if(isset($arr[0]) && $arr[0]===false){
		unset($arr[0]);
	} else if(!isset($arr[0])){
		$name = "== 请选择 ==";
    	echo "<option value=''>$name</option>";
	} else {
		$name =  $arr[0];
		unset($arr[0]);
    	echo "<option value=''>$name</option>";
	}
    
    if(is_array(current($arr))){
    	 foreach ($arr as $group_name => $items) {
			echo "<optgroup label='" . h($group_name) . "'>";
			foreach ($items as $id => $title){
				
				echo "<option value='$id'";
	        	if (in_array($id,$selected)) { echo ' selected'; }
	        	echo '>' . h($title) . "</option>";
			}
			echo "</optgroup>";
	    }
    }else{
	    foreach ($arr as $value => $title) {
	        echo '<option value="' . h($value) . '"';
	        if (in_array($value,$selected)) { echo ' selected'; }
	        echo '>' . h($title) . "</option>";
	    }
    }
    echo "</select>";
}

/**
 * 生成一组单选框
 *
 * @param string $name
 * @param array $arr
 * @param mixed $checked
 * @param string $separator
 * @param string $extra
 */
function html_radio_group($name, $arr, $checked = null, $separator = '', $extra = null) {
    $ix = 0;
    foreach ($arr as $value => $title) {
        $value_h = h($value);
        $title = t($title);
        echo "<input name=\"{$name}\" type=\"radio\" id=\"{$name}_{$ix}\" value=\"{$value_h}\" ";
        if ($value == $checked) {
            echo "checked=\"checked\"";
        }
        echo " {$extra} />";
        echo "<label for=\"{$name}_{$ix}\">{$title}</label>";
        echo $separator;
        $ix++;
        echo "\n";
    }
}

/**
 * 生成一组多选框
 *
 * @param string $name
 * @param array $arr
 * @param array $selected
 * @param string $separator
 * @param string $extra
 */
function html_checkbox_group($name, $arr, $selected = array(), $separator = '', $extra = null) {
    $ix = 0;
    if (!is_array($selected)) {
        $selected = array($selected);
    }
    foreach ($arr as $value => $title) {
        $value_h = h($value);
        $title = t($title);
        echo "<input name=\"{$name}[]\" type=\"checkbox\" id=\"{$name}_{$ix}\" value=\"{$value_h}\" ";
        if (in_array($value, $selected)) {
            echo "checked=\"checked\"";
        }
        echo " {$extra} />";
        echo "<label for=\"{$name}_{$ix}\">{$title}</label>";
        echo $separator;
        $ix++;
        echo "\n";
    }
}

/**
 * 生成一个多选框
 *
 * @param string $name
 * @param int $value
 * @param boolean $checked
 * @param string $label
 * @param string $extra
 */
function html_checkbox($name, $value = 1, $checked = false, $label = '', $extra = null) {
    echo "<input name=\"{$name}\" type=\"checkbox\" id=\"{$name}_1\" value=\"{$value}\"";
    if ($checked) { echo " checked"; }
    echo " {$extra} />\n";
    if ($label) {
        echo "<label for=\"{$name}_1\">" . h($label) . "</label>\n";
    }
}

/**
 * 生成一个单选框
 *
 * @param string $name
 * @param int $value
 * @param boolean $checked
 * @param string $label
 * @param string $extra
 */
function html_radio($name, $value = 1, $checked = false, $label = '', $extra = null) {
    echo "<input name=\"{$name}\" type=\"radio\" id=\"{$name}_1\" value=\"{$value}\"";
    if ($checked) { echo " checked"; }
    echo " {$extra} />\n";
    if ($label) {
        echo "<label for=\"{$name}_1\">" . h($label) . "</label>\n";
    }
}

/**
 * 生成一个文本输入框
 *
 * @param string $name
 * @param string $value
 * @param int $width
 * @param int $maxLength
 * @param string $extra
 */
function html_textbox($name, $value = '', $width = null, $maxLength = null, $extra = null) {
    echo "<input name=\"{$name}\" id=\"{$name}\" type=\"text\" value=\"" . h($value) . "\" ";
    if ($width) {
        echo "size=\"{$width}\" ";
    }
    if ($maxLength) {
        echo "maxlength=\"{$maxLength}\" ";
    }
    echo " {$extra} />\n";
}

/**
 * 生成一个密码输入框
 *
 * @param string $name
 * @param string $value
 * @param int $width
 * @param int $maxLength
 * @param string $extra
 */
function html_password($name, $value = '', $width = null, $maxLength = null, $extra = null) {
    echo "<input name=\"{$name}\" type=\"password\" value=\"" . h($value) . "\" ";
    if ($width) {
        echo "size=\"{$width}\" ";
    }
    if ($maxLength) {
        echo "maxlength=\"{$maxLength}\" ";
    }
    echo " {$extra} />\n";
}

/**
 * 生成一个多行文本输入框
 *
 * @param string $name
 * @param string $value
 * @param int $width
 * @param int $height
 * @param string $extra
 */
function html_textarea($name, $value = '', $width = null, $height = null, $extra = null) {
    echo "<textarea name=\"{$name}\"";
    if ($width) { echo "cols=\"{$width}\" "; }
    if ($height) { echo "rows=\"{$height}\" "; }
    echo " {$extra} >";
    echo h($value);
    echo "</textarea>\n";
}

/**
 * 生成一个隐藏域
 *
 * @param string $name
 * @param string $value
 * @param string $extra
 */
function html_hidden($name, $value = '', $extra = null) {
    echo "<input name=\"{$name}\" type=\"hidden\" value=\"";
    echo h($value);
    echo "\" {$extra} />\n";
}

/**
 * 生成一个文件上传域
 *
 * @param string $name
 * @param string $oldFile
 * @param int $width
 * @param string $extra
 */
function html_filefield($name,$oldFile=null, $width = null, $extra = null) {
    echo "<input name=\"{$name}\" type=\"file\"";
    if ($width) {
        echo " size=\"{$width}\"";
    }
    echo " {$extra} />";
    if (!empty($oldFile)) {
    	echo "<a href='$oldFile' target='_blank'>查看已上传文件</a>";
    }
}

/**
 * 生成一个Html编辑器
 */
function html_editor($name,$value=null,$toolbarSet=null,$width=null,$height=null,$extra = null){
	$obj = new Helper_Editor($name);
	$width && $obj->Width = $width;
	$height && $obj->Height = $height;
	$value && $obj->Value = $value;
	$toolbarSet && $obj->ToolbarSet	= $toolbarSet ;
	
	$obj->Config['AutoDetectLanguage']	= false ;
	$obj->Config['DefaultLanguage']		= 'zh-cn' ;
	$obj->Config['DefaultLanguage']		= 'zh-cn' ;
	$_SESSION['EDITOR']["UPLOAD_PATH"] = PATH_APP_UPLOAD;
	$_SESSION['EDITOR']["UPLOAD_ENABLE"] = true;
	$_SESSION['EDITOR']["VIEW_PATH"] = URL_WEB . "Upload/";
	$obj->Create();
}
/**
 * 显示html编辑器编辑后的内容。
 * 这样会添加分页信息等其其它内容
 * @param unknown_type $content
 */
function html_editor_show($content){
	$content = explode('<div style="page-break-after: always"><span style="display: none">&nbsp;</span></div>',$content);
	$total = count($content);
	if($total > 1){
		$pager = new Pagination($total,1,_gp('page','int'));
		$pager_info =  "<table align=center><td><div class='fanye_out'>" . $pager->display . "</div></td></table>";
		$current = $pager->minimum;
	} else {
		$current = 0;
	}
	echo $content[$current] .$pager_info;
}
/**
 * 转换 HTML 特殊字符，等同于 htmlspecialchars()
 *
 * @param string $text
 *
 * @return string
 */
function h($text)
{
    return htmlspecialchars($text);
}

/**
 * 转换 HTML 特殊字符以及空格和换行符
 *
 * 空格替换为 &nbsp; ，换行符替换为 <br />。
 *
 * @param string $text
 *
 * @return string
 */
function t($text)
{
    return nl2br(str_replace(' ', '&nbsp;', htmlspecialchars($text)));
}
